登录 白背景

https://leetcode-cn.com/problems/longest-common-prefix/submissions/

最笨的遍历方法

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if len(strs) == 0:
            return ''
        #以第一个单词作为比较对象
        for l in range(1,len(strs[0])+1)[::-1]:
            flag = True
            #遍历其余所有单词
            for word in strs[1:]:
                if word[:l] != strs[0][:l]:
                    flag = False
            if flag:
                return strs[0][:l]
        return ''